<?php
class Application_Model_TileResources extends Xend_Db_Model {
	protected $_dbTableName = 'Application_Model_Table_TileResources';
	public function __construct() {
		parent::__construct();
	}

	/**
	 * speichert einen Datensatz vom Typ Application_Model_Schema_TileResource
	 * @param object Application_Model_Schema_TileResource
	 * @return int ID des Datensatzes in der Datenbanktabelle tile_resources
	 */
	public function save(Application_Model_Schema_TileResource $tileResource) {
		$ret	= null;
		$data = array(
			'resources_id'	=> $tileResource->resource->id,
			'cur_value'			=> $tileResource->resource->curValue
		);

		if($tileResource->id === null) {
			$ret = $this->getDbTable()->insert($data);

		} else {
			$ret = $this->getDbTable()->update($data, array('id = ?' => $tileResource->id));
		}

		return (int) $ret;
	}

	/**
	 * fragt eine TileResource Eintrag ab, der auf die eventuell uebergebenen Bedingungen
	 * zutrifft
	 * @param array $where Array mit Bedingungen, die an die Datenbank uebergeben werden
	 * @param array $ref Array mit Tabellen, die verknuepft werden sollen
	 * @return array Array mit den Daten, die aus der Datenbank geladen wurden
	 */
	public function find($where = array(), $ref = array()) {
		$tileResource = null;

		if(is_array($data = $this->findall($where, $ref, array('limit' => 1)))) {
			$tileResource = current($data);
		}

		return $tileResource;
	}

	/**
	 * fragt alle TileResource Eintraege, die auf die eventuell uebergebenen Bedingungen
	 * zutreffen
	 * @param array $where Array mit Bedingungen, die an die Datenbank uebergeben werden
	 * @param array $ref Array mit Tabellen, die verknuepft werden sollen
	 * @return array Array mit den Daten, die aus der Datenbank geladen wurden
	 */
	public function findall($where = array(), $ref = array(), $options = array()) {
		$tileResources	= array();
		$ref						= array_flip($ref);
		$select					= $this->_database
			->select()
			->from(
				array('tile_resources'),
        array(
					'tile_resources.id', 'tile_resources.tiles_id', 'tile_resources.resources_id', 'tile_resources.cur_value'
				)
			);

		// Referenzen
		// -----------------------------------------------------------------------------------
		if(array_key_exists('Resources', $ref)) {
			$select
				->joinLeft('resources', 'tile_resources.resources_id = resources.id', null)
				->columns(array('name', 'max_value'), 'resources');
		}

		// Where
		// -----------------------------------------------------------------------------------
		if(is_array($where) && !empty($where)) {
			foreach($where as $k => $v) {
				$select->where($k, $v);
			}
		}

		// Options
		// -----------------------------------------------------------------------------------
		if(isset($options['limit'])) {
			$select->limit($options['limit'], 0);
		}

		// Datenauswertung
		// -----------------------------------------------------------------------------------
		foreach($this->fetch($select) as $k => $v) {

			// Tile
			// ---------------------------------------------------------------------------------
			$tile = new Application_Model_Schema_Tile();
			$tile->setId($v['tile_resources']['tiles_id']);

			// Resource
			// ---------------------------------------------------------------------------------
			$resource	= new Application_Model_Schema_Resource();
			$resource
				->setId($v['tile_resources']['resources_id'])
				->setCurValue($v['tile_resources']['cur_value']);

			if(array_key_exists('Resources', $ref)) {
				$resource
					->setName($v['resources']['name'])
					->setMaxValue($v['resources']['max_value']);
			}

			// TileResource
			// ---------------------------------------------------------------------------------
			$tileResource	= new Application_Model_Schema_TileResource();
			$tileResource
				->setId($v['tile_resources']['id'])
				->setResource($resource)
				->setTile($tile);

			$tileResources[(int) $v['tile_resources']['id']] = $tileResource;
		}

		// Actions
		// -----------------------------------------------------------------------------------
		if(array_key_exists('Actions', $ref)) {
			$tileResources = $this->_assignActionReferences($tileResources);
		}

		return $tileResources;
	}

	/**
	 * verknuepft die, per Array uebergebenen, Resource-Eintraege mit den Daten der
	 * referenzierten Actions (action_use_resources)
	 * @param array $sources Array mit den Daten der Resourcen
	 * @param array $references optionales Array mit den UID der Resourcen
	 * @return array Array der Resourcen mit den verknuepften Actions-Eintraegen
	 */
	protected function _assignActionReferences($sources, $references = array()) {
		if(empty($references)) {
			foreach($sources as $source) {
				$references[$source->resource->id] = $source->id;
			}
		}

		$select = $this->_database
			->select()
			->from(
				array('actions'),
        array('actions.id', 'actions.name', 'action_use_resources.resources_id')
			)
			->join('action_use_resources', 'actions.id = action_use_resources.actions_id', null)
			->where('action_use_resources.resources_id IN (?)', array_keys($references));

		// Datenauswertung
		// -----------------------------------------------------------------------------------
		foreach($this->fetch($select) as $k => $v) {
			$resourceId = (int) $v['action_use_resources']['resources_id'];

			if(array_key_exists($resourceId, $references)) {
				$action = new Application_Model_Schema_Action();
				$action
					->setId($v['actions']['id'])
					->setName($v['actions']['name']);

				$sources[$references[$resourceId]]->setAction($action, $action->id);
			}
		}

		return $sources;
	}
}
?>